<?php

/**
 * 基础订餐类
 * Created by PhpStorm.
 * User: allen_ios
 * Date: 17/6/12
 * Time: 下午11:08
 */
class BaseRestaurantService extends BaseService
{

    const REDIS_CACHE_EXPIRE = 600;
    private $CategoryModel;  // 分类模型
    private $restaurantId;     //餐厅id
    private $resId; // 模块id
    private $terminal; // 平板终端id
    private $windowid; // 模块id
    public function __construct()
    {

        $params = func_get_args();


        //$this->resId = $params[0][0];
       // $this->restaurantId = $params[0][1];
        $this->terminal = $params[0][2];
        //根据sn查找生成可配的restrant_id

        $this->windowid=M('RestaurantstyyWindowSn')->where('sn_num='."'$this->terminal'")->getField('window_id');


        $this->restaurantId=M('RestaurantstyyWindow')->where('window_id='.$this->windowid)->getField('restaurant_id');
        $this->resId=  M('RestaurantstyyInfo')->where('restaurant_id='. $this->restaurantId)->getField('res_id');

        $this->CategoryModel = D('Base/RestaurantstyyGoodsCategory');
        $this->WindowSn = D('Base/RestaurantstyyWindowSn');
        $this->cacheKey =  'zdu_restaurantstyy_category_'.$this->resId.'_'.$this->restaurantId;

    }

    /**
     * 根据模块id和餐厅id获取分类信息
     * @return mixed
     */
    public function getCategory()
    {
        /*//先取缓存，没有就取数据库再存入缓存expire = 600s;
        $cacheData = RedisService::get($this->cacheKey);
        if(!$cacheData){*/

        $windowId = $this->WindowSn->where(array('sn_num' => $this->terminal, 'cut_off' => 0))->field('window_id')->select();

        if ($windowId) {
            foreach ($windowId as $k => $v) {
                $windowIds[] = $v['window_id'];
            }
            $windowIds = implode(',', $windowIds);
            $condition = array(
                'res_id' => $this->resId,
                'restaurant_id' => $this->restaurantId,
                 'window_id' => array('in', $windowIds),
                'cut_off' => 0
            );
            $field = 'category_id,category_name,category_name_eg';
            $order = 'sort asc';
            $category = $this->CategoryModel->getList($condition, $field, $order);
//            var_dump(  $category);
           // echo  $category->getLastSql();
          
            $mGoods = D('Base/RestaurantstyyGoods');
            //找出各自分类下面的菜品
            foreach ($category as $k => $v) {
                $condition = array(
                    'res_id' => $this->resId,
                    'restaurant_id' => $this->restaurantId,
                    'category_id' => $v['category_id'],
                    'cut_off' => 0,
                    'goods_sum' => array('gt', 0),
                    
                );

                $category[$k]['goods'] = $mGoods->where($condition)->field('goods_id,goods_name,goods_name_eg,price,goods_logo,goods_sum,print_type')->order($order)->select();


            }
        
            return $category;

        }
    }

    /**
     * 获得单个餐厅
     * @param int $resId    餐厅模块Id
     */
    protected function getRestaurantInfo($resId)
    {
        return array();
    }

    /**
     * 获得档口
     * @param int $resId 餐厅模块id
     * @param int $restaurantId 餐厅id
     */
    protected function getRestaurantWindows($resId,$restaurantId)
    {
        return array();
    }

    /**
     * 打印操作
     * @param $orderInfo
     */
    protected function doPrint($orderInfo)
    {

    }
}